﻿layui.extend({
    unobtrusive:'lib/unobtrusive'
}).define(['table', 'form', 'jquery', 'layer','laytpl','index','unobtrusive','permisson'], function (exports) {
    var table = layui.table,
        $ = layui.$,
        laytpl = layui.laytpl,
        index = layui.index,
        form = layui.form,
        permisson = layui.permisson,
        layer = parent === self ? layui.layer : top.layui.layer;

    var pageName = layui.cache.module.pageName;

    //用户管理主界面
    if (pageName === 'indexPage') {

        //初始化用户数据
        var userTable = table.render({
            elem: '#data_User',
            page: true,
            url: '/UserManage/UserList',
            cols: [[
                { field: 'NickName', title: '用户昵称', width: '15%' },
                {
                    field: 'Sex',
                    title: '性别',
                    width: '10%',
                    align: 'center',
                    templet: function (data) {
                        var sex = data.Sex;
                        if (sex === 10) {
                            return '<div><span style="color:blue"><i class="layui-icon layui-icon-male"></i>男</span></div>';
                        } else if (sex === 20) {
                            return '<div><span style="color:pink"><i class="layui-icon layui-icon-female"></i>女</span></div>';
                        } else {
                            return '未知';
                        }
                    }
                },
                { field: 'PhoneNumber', title: '手机号码', align: 'center', width: '15%' },
                { field: 'Email', title: '邮箱', width: '18%' },
                {
                    field: 'Status', title: '状态', width: '10%', align: 'center', templet: function (data) {
                        var isBanned = data.IsBanned;
                        if (isBanned) {
                            return '<div><span style="color:red">禁用</span></div>';
                        } else {
                            return '<div><span style="color:green">正常</span></div>';
                        }
                    }
                },
                { field: 'Switch', title: '切换状态', width: '12%', templet: '#tpl_UserStatus' },
                { field: 'Operate', title: '操作', templet: '#tpl_Operate' }
            ]],

            //表格渲染成功的回调函数
            done: function () {
                permisson.controllPermisson();
            }
        });

        //监听用户状态开关改变事件
        form.on('switch(userStatus)',
            function (obj) {
                var userId = obj.value;
                $.post('/Admin/UserManage/ChangeUserStatus',
                    {
                        userId: userId
                    },
                    function (data) {
                        //重载表格内容
                        layer.alert(data.msg, {
                            icon: 2,
                            title: '错误提示'
                        });
                        userTable.reload();
                    });
            });
        //工具栏监听事件
        table.on('tool(userManage)',
            
            function (obj) {
                var userData = obj.data,
                    event = obj.event;

                if (event === 'selectRole') {
                    layer.open({
                        type: 2,
                        title: '选择用户角色',
                        area: ['720px', '480px'],
                        content: '/Admin/UserManage/SelectRole?userId=' + userData.UserId
                    });
                }
                //选择用户权限
                if (event==='selectClaim') {
                    layer.open({
                        type: 2,
                        title: '选择用户权限',
                        area: ['720px', '480px'],
                        content: '/Admin/UserManage/SelectClaim?userId=' + userData.UserId,
                        end: function () {
                            //重载表格内容
                            userTable.reload();
                        }
                    });
                }
            });
    }

    //选择角色界面
    if (pageName === 'selectRolePage') {
        var userId = $('#userId').val();
        table.render({
            elem: '#data_UserRole',
            url: '/Admin/UserManage/UserRoleList',
            page: true,
            where: {
                userId: userId
            },
            cols: [[
                { type: 'checkbox' },
                { field: 'RoleName', title: '角色名称', width: '30%' },
                { field: 'Description', title: '角色描述' }
            ]]
        });

        //保存用户角色按钮点击事件
        $('#btn_SelectRole').click(function () {
            //获取选中的数据
            
            var checkedData = table.checkStatus('data_UserRole');

            $.post('/Admin/UserManage/SelectRole',
                {
                    checkedRole: checkedData.data,
                    userId: userId
                },
                function () {
                    var layerIndex = layer.getFrameIndex(window.name);
                    layer.close(layerIndex);
                });
        });
    }
    //选择声明页面
    if (pageName === 'selectClaimPage') {
        var selectUserId = $('#userId').val();

        //初始化声明数据
        $.post('/Admin/UserManage/ClaimList',
            {
                userId: selectUserId
            },
            function (rtnResult) {
                if (rtnResult.code !== 0) {
                    layer.msg(rtnResult.msg);
                } else {
                    var claimTpl = $('#tpl_Claim').html();
                    var $claim = $('#cho_Claim');

                    laytpl(claimTpl).render(rtnResult.data,
                        
                        function (html) {
                            $claim.append(html);
                            form.render('checkbox');
                        });
                }
            });

        window.onRequestSelectClaimComplete = function (res) {
            index.handleCallbackResult(res,
                function () {
                    var layerIndex = layer.getFrameIndex(window.name);
                    layer.close(layerIndex);
                });
        }
    }
    exports('userManage', null);
})